package com.gem.controller;

import com.gem.domain.CompanyContact;
import com.gem.service.CCrParticipantService;
import com.gem.service.CompanyContactService;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 参与人控制器
 * create 2019/10/30 16:40
 * Modified By:
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/cp")
public class CCrParticipantController {

    //自动注入CCrParticipantService
    private CCrParticipantService cCrParticipantService;

    @Autowired
    public void setcCrParticipantService(CCrParticipantService cCrParticipantService) {
        this.cCrParticipantService = cCrParticipantService;
    }

    //自动注入JSONResultUtil
    private JSONResultUtil result;

    @Autowired
    public void setResult(JSONResultUtil result) {
        this.result = result;
    }

    //自动注入
    private CompanyContactService companyContactService;

    @Autowired
    public void setCompanyContactService(CompanyContactService companyContactService) {
        this.companyContactService = companyContactService;
    }

    //首页显示
    @RequestMapping("/cpShow")
    public String cpShow(String ccCrId, String ccId, Integer comId, String ccrcDate, String ccrcTopic, String ccrcSales, String ccrcCourseTeacher,
                         @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                         @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                         Model model) {
        try {
            List<CompanyContact> companyContactList = cCrParticipantService.selectConByccIdService(Integer.parseInt(ccId));
            if (companyContactList.size() > 0) {
                model.addAttribute("contactList", companyContactList);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("ccCrId", ccCrId);
            model.addAttribute("ccId", ccId);
            model.addAttribute("comId", comId);
            model.addAttribute("ccrcDate", ccrcDate);
            model.addAttribute("ccrcCourseTeacher", ccrcCourseTeacher);
            model.addAttribute("ccrcTopic", ccrcTopic);
            model.addAttribute("ccrcSales", ccrcSales);
            model.addAttribute("currentPage", currentPage);
            model.addAttribute("pageSize", pageSize);
            return "company/result_participant";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/cc/ccShow?ccCrId=" + ccCrId, "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //单个删除
    @RequestMapping(value = "/cpDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil crDelete(String cpId) {
        try {
            if (cpId != null && !cpId.trim().equals("")) {
                int cp_Id = Integer.parseInt(cpId);
                if (cCrParticipantService.deleteCPService(cp_Id)) {
                    result.setId(0);
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("服务器异常，删除失败哦！");
                }
            } else {
                result.setId(-1);
                result.setData("服务器异常，没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //批量删除
    @RequestMapping(value = "/cpsDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil cpsDelete(String cpIdStr) {
        try {
            String[] cpIdArr = cpIdStr.split(",");
            if (cpIdArr.length != 0) {
                if (cCrParticipantService.deleteCPsService(cpIdArr)) {
                    result.setId(0);
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("删除发生异常，请检查服务器！");
                }
            } else {
                result.setId(-1);
                result.setData("没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //添加参与人
    @RequestMapping(value = "/cpAdd", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil cpAdd(CompanyContact companyContact) {
        try {
            if (cCrParticipantService.addCPService(companyContact, companyContact.getCcId() + "")) {
                result.setId(0);
                result.setData("添加成功！");
            } else {
                result.setId(1);
                result.setData("添加未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //修改参与人
    @RequestMapping(value = "/cpModify", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil cpModify(CompanyContact companyContact) {
        try {
            if (companyContactService.update(companyContact) > 0) {
                result.setId(0);
                result.setData("修改成功！");
            } else {
                result.setId(1);
                result.setData("修改未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //查询参与人
    @RequestMapping(value = "/cpSearch", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil cpSearch(String coContactNameAdd) {
        try {
            CompanyContact companyContact = companyContactService.selectConByName(coContactNameAdd);
            if (companyContact != null) {
                result.setId(0);
                result.setData(companyContact);
            }else{
                result.setId(1);
                result.setData("无");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }
}
